<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <parent-cpn></parent-cpn>
  </div>
  <!-- 父组件 -->
  <template id="parentCpn">
    <div>
      <h2>我是父组件</h2>
      <child-cpn></child-cpn>
    </div>
  </template>

  <!-- 子组件 -->
  <template id="childCpn">
    <div>
      <h2>我是子组件</h2>
      <button @click="btnClick">访问父组件</button>
    </div>
  </template>
  <script src="../js/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        message: '我是root组件的message'
      },
      components: {
        'parent-cpn': {
          template: '#parentCpn',
          data() {
            return {
              name: '我是父组件的name'  
            }
          },
          components: {
            'child-cpn': {
              template: '#childCpn',
              methods: {
                btnClick(){
                  // console.log('我只子组件的点击方法')
                  // 1.访问父组件$parent
                  console.log(this.$parent.name);
                  // 2.访问root组件
                  console.log(this.$root.message);
                }
              },
            }
          }
        }
      }
  })
  </script>
</body>
</html>